import Mock, { Random } from 'mockjs'
import setupMock, {
    successResponseWrap,
    failResponseWrap
} from '@/utils/setup-mock'
import qs from 'query-string'

import { MockParams } from '@/types/global'
import {
    AUTH_USER_PROFILE,
    AUTH_USER_LIST,
    AUTH_USER_LOGIN,
    AUTH_USER_LOGOUT,
    AUTH_USER_LOGIN_CAPTCHA
} from '@/config/api'
import { AUTH_USER_TOKEN_KEY } from '@/config/constant'

const userList = Mock.mock({
    'list|55': [
        {
            'id|+1': 1,
            avatar: Random.image(
                '200x200',
                Random.color(),
                Random.color(),
                'png',
                'T'
            ),
            username: '@lower(@first)',
            'state|1': [0, 1],
            create_time: Random.datetime(),
            '_protect|1': [0, 1]
        }
    ]
})

setupMock({
    setup() {
        // 登录验证码
        Mock.mock(new RegExp(AUTH_USER_LOGIN_CAPTCHA), () => {
            return successResponseWrap({
                image: Random.image(
                    '150x38',
                    Random.color(),
                    '#FFF',
                    'png',
                    'captcha'
                ),
                expire: Random.integer(100, 1800) // 秒
            })
        })

        // 用户列表
        Mock.mock(new RegExp(AUTH_USER_LIST), (params: MockParams) => {
            const isLogin = !!localStorage.getItem(AUTH_USER_TOKEN_KEY)
            if (!isLogin) return failResponseWrap('未登录', 203)

            const PM = qs.parseUrl(params.url).query
            console.log('PM', PM)

            const { page = 1, limit = 10 } = qs.parseUrl(params.url).query
            const p = Number(page)
            const ps = Number(limit)

            return successResponseWrap({
                data: userList.list.slice((p - 1) * ps, p * ps),
                total: 55,
                per_page: ps,
                current_page: p,
                last_page: Math.ceil(55 / ps)
            })
        })

        // 用户信息
        Mock.mock(new RegExp(AUTH_USER_PROFILE), () => {
            const isLogin = !!localStorage.getItem(AUTH_USER_TOKEN_KEY)
            if (isLogin) {
                return successResponseWrap(
                    Mock.mock({
                        id: '@id',
                        'role|0-3': ['AMD', 'CMD', 'UMD'],
                        username: '@first',
                        nickname: '@cname',
                        avatar: '@image("200x200", "#f60", "#FFF", "图")',
                        mobile: '150****0000',
                        email: '@email',
                        'state|1': [0, 1],
                        motto: '@cparagraph',
                        create_time: '@date("yyyy-MM-dd") @time("HH:mm:ss")'
                    })
                )
            }

            return failResponseWrap('未登录', 203)
        })

        // 登录
        Mock.mock(new RegExp(AUTH_USER_LOGIN), (params: MockParams) => {
            const { username, password } = JSON.parse(params.body)
            if (!username) {
                return failResponseWrap('用户名不能为空', 50000)
            }
            if (!password) {
                return failResponseWrap('密码不能为空', 50000)
            }

            if (username !== 'root' || password !== '123123') {
                return failResponseWrap('账号或者密码错误', 50000)
            }

            return successResponseWrap({
                token: Random.guid(),
                expire: Random.integer(),
                refresh_expire: Random.integer(),
                refresh_token: Random.guid()
            })
        })

        // 登出
        Mock.mock(new RegExp(AUTH_USER_LOGOUT), () => {
            return successResponseWrap(null)
        })
    }
})
